import os
import sys

import parsel
import pymysql

sys.path.append("..")

from src import facade
from src.facade import configdicts
from src.mylogger import MLogger
from src.mysqlfacade import MysqlUtiles

abspath = os.path.dirname(os.path.abspath(__file__))
configpath = os.path.join(abspath, "db.ini")


class ParaJournal(object):

    def __init__(self):
        self.logger = MLogger().streamlogger
        self.listdir = configdicts["path"]["path_step3"]
        self.mysqlutile = MysqlUtiles(configpath, "db", logger=self.logger)

    def para_all(self):
        for file in facade.get_dir_all_files(self.listdir):
            filename = os.path.split(file)[-1]
            filename = os.extsep.join(filename.split(os.extsep)[:-1])
            html = facade.single_read_file(file)
            self.para(html, filename)

    def para(self, html, filename):
        select = parsel.Selector(text=html)
        pmsg = select.xpath('//*[@id="tdInfo"]/p[2]').get()
        pmsg = pmsg.replace("<p>", "").replace("<strong>", "").replace("</strong>", "").replace("</p>", "").replace(
            "\xa0", "")
        if not pmsg.find("该刊被以下数据库收录") > -1:
            return
        pmsg_list = pmsg.split("<br>")
        print(pmsg_list)

        type_name = ''
        pub_place = ''
        language = ''
        book_size = ''
        issn = ''
        cnno = ''
        emailcode = ''
        create_date = ''
        collect_abbr = ''
        core_journal = ''
        if "该刊被以下数据库收录：" in pmsg_list:
            if "核心期刊：" in pmsg_list:
                for msg in pmsg_list[pmsg_list.index("该刊被以下数据库收录："):pmsg_list.index("核心期刊：")]:
                    if msg.strip():
                        if msg != "该刊被以下数据库收录：":
                            collect_abbr = collect_abbr + ";" + msg
                collect_abbr = collect_abbr.lstrip(";")

                for msg in pmsg_list[pmsg_list.index("核心期刊："):]:
                    if msg.strip():
                        if msg != "核心期刊：":
                            core_journal = core_journal + ";" + msg
                core_journal = core_journal.lstrip(";")


            else:
                for msg in pmsg_list[pmsg_list.index("该刊被以下数据库收录："):]:
                    if msg.strip():
                        if msg != "该刊被以下数据库收录：":
                            collect_abbr = collect_abbr + ";" + msg
                collect_abbr = collect_abbr.lstrip(";")

        elif "核心期刊：" in pmsg_list:
            for msg in pmsg_list[pmsg_list.index("核心期刊："):]:
                if msg.strip():
                    if msg != "核心期刊：":
                        core_journal = core_journal + ";" + msg
            core_journal = core_journal.lstrip(";")

        for msg in pmsg_list:
            if msg.find("周期") > -1:
                type_name = msg.replace("周期：", "").strip()
            elif msg.find("出版地") > -1:
                pub_place = msg.replace("出版地：", "").strip()
            elif msg.find("语种") > -1:
                language = msg.replace("语种：", "").strip()
            elif msg.find("开本") > -1:
                book_size = msg.replace("开本：", "").strip()
            elif msg.find("ISSN") > -1:
                issn = msg.replace("ISSN：", "").strip()
            elif msg.find("CN") > -1:
                cnno = msg.replace("CN：", "").strip()
            elif msg.find("邮发代号") > -1:
                emailcode = msg.replace("邮发代号：", "").strip()
            elif msg.find("创刊时间") > -1:
                create_date = msg.replace("创刊时间：", "").strip()
        pub_place = pymysql.escape_string(pub_place)
        collect_abbr = pymysql.escape_string(collect_abbr)
        core_journal = pymysql.escape_string(core_journal)
        sql = "update journal set type_name='{}',pub_place='{}',`language`='{}',book_size='{}',issn='{}',cnno='{}',emailcode='{}',create_date='{}',collect_abbr='{}',core_journal='{}' where baseid='{}'"
        sql = sql.format(type_name, pub_place, language, book_size, issn, cnno, emailcode, create_date, collect_abbr,
                         core_journal, filename)
        self.mysqlutile.ExeSqlToDB(sql)

        li_list = select.xpath('.//ul[@class="list1"]/li')
        Listsql = []
        for li in li_list:
            url = li.xpath('.//a/@href').get()
            year = li.xpath('.//a/text()').get()
            year = year.replace("年", "")
            sql = "insert ignore into `year`(url,baseid,`year`) values ('%s','%s','%s')"
            sql = sql % (url, filename, year)
            Listsql.append(sql)
        self.mysqlutile.ExeSqlListToDB(Listsql)


if __name__ == "__main__":
    para = ParaJournal()
    para.para_all()
